package com.hogan.hoganproject.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.android.tu.loadingdialog.LoadingDailog;
import com.hogan.hoganproject.R;
import com.hogan.hoganproject.base.mvp.BasePresenter;
import com.hogan.hoganproject.base.utils.HoganContacts;

/**
 * Author: chenhu
 * Time: 2020/8/31 10:32
 * Description do somethings
 */
public abstract class BaseFragment<P extends BasePresenter> extends Fragment {
    protected P mFragmentPresenter;
    protected View mBaseView;
    protected Context mContext;
    public LoadingDailog mLoadingDialog;
    private Handler baseHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (HoganContacts.DELAYED_LOAD_DATA == msg.what) {
                onRecoverLoadData();
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoadingDialog = new LoadingDailog.Builder(getActivity())
                .setMessage(getString(R.string.app_settings))
                .setCancelable(true)
                .setCancelOutside(true)
                .create();

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (getLayoutResID() == 0) {
            throw new NullPointerException("layout is not empty");
        }
        mContext = getActivity();
        mBaseView = inflater.inflate(getLayoutResID(), container, false);
        mFragmentPresenter = initPresenter();
        initView();
        return mBaseView;
    }

    public abstract int getLayoutResID();

    protected abstract P initPresenter();

    protected abstract void initView();

    /**
     * fragment hide，回收相关操作
     */
    public abstract void onUnsubscribe();//hide时，取消

    /**
     * fragment show，数据重新加载相关
     */
    public abstract void onRecoverLoadData();

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            if (baseHandler != null) {
                baseHandler.removeMessages(HoganContacts.DELAYED_LOAD_DATA);
            }
            onUnsubscribe();
        } else {
            //延时再去更新数据，不然会造成view为空
            if (baseHandler != null) {
                baseHandler.sendEmptyMessageDelayed(HoganContacts.DELAYED_LOAD_DATA, HoganContacts.DELAYED_LOAD_DATA_TIME);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (baseHandler != null) {
            baseHandler.removeMessages(HoganContacts.DELAYED_LOAD_DATA);
        }
    }
}
